package com.sx.shop.controller;

import com.sx.shop.entity.Category;
import com.sx.shop.service.CategoryService;
import com.sx.shop.vo.CategoryTreeVo;
import com.sx.shop.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/category")
public class CategoryController
{
    @Autowired
    private CategoryService categoryService;
    @RequestMapping("/index")
    public String index()
    {
        return "category";
    }
    //添加一个类
    @RequestMapping("/save")
    @ResponseBody
    public String addCateGory(Category category)
    {
        return categoryService.addCateGory(category.getParent_id(), category.getName());
    }
    //删除通过id
    @RequestMapping("/del")
    @ResponseBody
    public String deleteCateGory(Integer id)
    {
        if(id!=null)
        {
           return categoryService.doDel(id);
        }
        return "操作失败";
    }
    //更新
    @RequestMapping("/update")
    @ResponseBody
    public String updateCateGory(Category category)
    {
        return categoryService.doSave(category);
    }
    //查看所有的一级类别
    @RequestMapping("/top")
    @ResponseBody
    public List<Category>selectTopCategory(){
        return categoryService.selectByParentId(0);
    }
    @ResponseBody
    @RequestMapping(value = "/tree")
    //商品的id 给它绑定一个类别
    public List<CategoryTreeVo> selectMenuTree(Integer id)
    {
        return categoryService.selectCategoryTree(id);
    }
    @GetMapping(value = "/list")
    @ResponseBody
    public ResultVo selectCategory(String keyword, Integer page , Integer limit) {
        return categoryService.selectCateGory(keyword,  page ,  limit);
    }
    @RequestMapping("/findById")
    @ResponseBody
    public Category findById(Integer id)
    {
        return categoryService.selectById(id);
    }
}
